package org.myblog.myblogserver.generator;


import org.apache.commons.lang.StringUtils;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.myblog.myblogserver.generator.model.TableInfo;
import org.myblog.myblogserver.generator.template.TemplateInitializer;
import org.myblog.myblogserver.generator.template.TemplateUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class CodeGenerator {
    public static void main(String[] args){
        // 初始化Velocity引擎
        TemplateInitializer.init();

        // 从数据库获取表信息并生成代码（这里以user表为例，你可以根据需要循环遍历所有表）
        TableInfo tableInfo = createTableInfoFromDatabase("notice");

        // 准备模板上下文
        VelocityContext context = TemplateUtil.prepareContext(tableInfo);

        // 获取模板列表
        List<String> templateList = TemplateUtil.getTemplateList();

        for (String templatePath : templateList) {
            // 获取Velocity模板对象
            Template template = Velocity.getTemplate(templatePath);

            // 获取生成的文件名
            String fileName = TemplateUtil.getFileName(templatePath, tableInfo);

            // 生成代码文件
            TemplateUtil.generateCode(template, context, fileName);
        }
    }

    // 模拟创建TableInfo对象的方法（实际从数据库获取信息填充）
    private static TableInfo createTableInfoFromDatabase(String tableName) {
        List<TableInfo.ColumnInfo> columns = new ArrayList<>();

        String url = "jdbc:mysql://localhost:3306/blogsystem";
        String username = "root";
        String password = "root";

        Connection connection = null;
        List<String> fields = new ArrayList<>();
        try{
            Class.forName("com.mysql.cj.jdbc.Driver");
            // 建立数据库连接
            connection = DriverManager.getConnection(url, username, password);
            // 获取数据库元数据
            Statement statement = connection.createStatement();

            ResultSet resultSet = statement.executeQuery("SELECT * FROM "+tableName);
//             resultSet = connection.getMetaData();
            // 获取结果集的元数据
            ResultSetMetaData metaData = resultSet.getMetaData();
            int columnCount = metaData.getColumnCount();

            for (int i = 1; i <= columnCount; i++) {
                // 获取字段名
                String columnName = metaData.getColumnName(i);
                String javaType = getJavaTypeFromSqlType(metaData.getColumnType(i));
                columns.add(new TableInfo.ColumnInfo(columnName, javaType));
//                System.out.println("字段名: " + columnName);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        return new TableInfo(tableName, StringUtils.capitalize(tableName), "org.myblog.myblogserver.code", columns);
    }

    private static String getJavaTypeFromSqlType(int sqlType) {
        switch (sqlType) {
            case Types.INTEGER:
                return "Integer";
            case Types.BIGINT:
                return "Long";
            case Types.VARCHAR:
                return "String";
            case Types.DATE:
                return "Date";
            case Types.LONGVARCHAR:
                return "String";
            case Types.BINARY:
                return "byte[]";
            // 可以根据实际的数据库类型添加更多的映射
            default:
                return "Object";
        }
    }
}
